package com.wanban.download.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DownloadSQLiteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "WBVideoDownload.db";
    public static final int DATABASE_VERSION = 3;
    public static final String TABLE_VIDEO_DOWNLOAD_INFO = "VideoTable";

    public static class Columns {
        public static final String URL = "url";
        public static final String VIDEO_NAME = "videoName";
        public static final String STATE = "state";
        public static final String VIDEO_ID = "videoId";
        public static final String EPISODE = "episode";
        public static final String SOURCE_ID = "sourceId";
        public static final String CATEGORY = "category";
        public static final String PERCENT = "percent";
        public static final String WATCH_TIME = "watchTime";
        public static final String CREATE_TIME = "createTime";
        public static final String DOWNLOAD_TYPE = "downloadType";
        public static final String CONTENT_LENGTH = "contentLength";
        public static final String PLAY_PATH = "playPath";
        public static final String EPISODE_NAME = "episodeName";
        public static final String COVER_URL = "coverUrl";
        public static final String FILE_ROOT_PATH = "fileRootPath";
        public static final String COVER_FILE_PATH = "coverFilePath";
        public static final String ERROR_MESSAGE = "errorMessage";
        public static final String M3U8_FILE_PATH = "m3u8_file_path";
        public static final String HEADER = "header";
        public static final String EXTRA = "extra";
        public static final String TEMP_CONTENT_LENGTH = "tempContentLength";
        public static final String SKITS_STATUS = "skitsStatus";
    }



    public DownloadSQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createVideoDownloadInfoTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1) {
            if (newVersion == 2) {
                db.execSQL("ALTER TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + " ADD COLUMN " + Columns.TEMP_CONTENT_LENGTH + " INTEGER");
            } else if (newVersion == 3) {
                db.execSQL("ALTER TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + " ADD COLUMN " + Columns.TEMP_CONTENT_LENGTH + " INTEGER");
                db.execSQL("ALTER TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + " ADD COLUMN " + Columns.SKITS_STATUS + " INTEGER");
            }
        } else if (oldVersion == 2) {
            if (newVersion == 3) {
                db.execSQL("ALTER TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + " ADD COLUMN " + Columns.SKITS_STATUS + " INTEGER");
            }
        }
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    private void createVideoDownloadInfoTable(SQLiteDatabase db) {
        // 删除旧表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO_DOWNLOAD_INFO);
        // 创建新表
        db.execSQL("CREATE TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + "("
                + Columns.URL + " TEXT PRIMARY KEY, "
                + Columns.VIDEO_NAME + " TEXT, "
                + Columns.STATE + " INTEGER, "
                + Columns.VIDEO_ID + " INTEGER, "
                + Columns.EPISODE + " INTEGER, "
                + Columns.SOURCE_ID + " INTEGER, "
                + Columns.CATEGORY + " INTEGER, "
                + Columns.PERCENT + " NUMBER, "
                + Columns.WATCH_TIME + " INTEGER, "
                + Columns.CREATE_TIME + " INTEGER, "
                + Columns.DOWNLOAD_TYPE + " INTEGER, "
                + Columns.CONTENT_LENGTH + " INTEGER, "
                + Columns.PLAY_PATH + " TEXT, "
                + Columns.ERROR_MESSAGE + " TEXT, "
                + Columns.COVER_URL + " TEXT, "
                + Columns.EPISODE_NAME + " TEXT, "
                + Columns.FILE_ROOT_PATH + " TEXT, "
                + Columns.COVER_FILE_PATH + " TEXT, "
                + Columns.M3U8_FILE_PATH + " TEXT, "
                + Columns.EXTRA + " TEXT, "
                // v2新增
                + Columns.TEMP_CONTENT_LENGTH + " INTEGER, "
                + Columns.SKITS_STATUS + " INTEGER, "
                + Columns.HEADER + " TEXT);");
    }

}
